<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/21 0021
 * Time: 下午 1:25
 */
namespace Admin\Controller;
use Org\Yike\Tree;
use Org\Yike\Pinyin;
class ShopController extends CommonController
{
    public function _initialize()
    {
        parent::_initialize();
        $this->_mod = D('shop');
    }
    public function index() {
        $tree = new Tree();
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $result = $this->_mod->get_list ();
        $array = array();
        foreach($result as $r) {
            $r['str_status'] = '<img data-tdtype="toggle" data-id="'.$r['id'].'" data-field="status" data-value="'.$r['status'].'" src="./Public/Admin/images/admin/toggle_' . ($r['status'] == 0 ? 'disabled' : 'enabled') . '.gif" />';
            $r['str_manage'] = '<a data-uri="'.U('Shop/delete',array('id'=>$r['id'])).'" data-msg="确定要删除 '.$r['title'].' 吗？" data-acttype="ajax" class="J_confirmurl" href="javascript:void(0);">删除</a> |
                                <a href="javascript:;" class="J_showdialog" data-uri="'.U('Shop/edit',array('id'=>$r['id'])).'" data-title="'.L('edit').' - '. $r['name'] .'" data-id="edit" data-width="500" data-height="360">'.L('edit').'</a>';
            $r['parentid_node'] = ($r['pid'])? ' class="child-of-node-'.$r['pid'].'"' : '';
            $r['cate_type'] = $r['type'] ? '<span class="blue">'.L('article_cate_type_'.$r['type']).'</span>' : L('article_cate_type_'.$r['type']);
            $array[] = $r;
        }
        $str  = "<tr id='node-\$id' \$parentid_node>
                <td align='center'><input type='checkbox' value='\$id' class='J_checkitem'></td>
                <td align='center'>\$id</td>
                <td align='center'>\$title</td>
				<td align='center'>\$name</td>
				<td align='center'>\$add_time</td>

                <td align='center'>\$str_status</td>
                <td align='center'>\$str_manage</td>
                </tr>";
        $tree->init($array);
        $list = $tree->get_tree(0, $str);
        $this->assign('list', $list);
        $big_menu = array(
            'title' => '添加店铺',
            'iframe' => U('Shop/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '360'
        );
        $this->assign('big_menu', $big_menu);
        $this->assign('list_table', true);
        $this->display();
    }
    /**
     * 入库数据整理
     */
    protected function _before_insert($data = '') {
        //检测分类是否存在
        $data['add_time'] = date ('Y-m-d H:i:s');
        return $data;
    }

    public function ajax_getchilds() {
        $id = I('id',0,'intval');
        if ($id <= 0)$return = $this->_mod->field('id,title as name')->select();
        if ($return) {
            $this->ajaxReturn(1, L('operation_success'), $return);
        } else {
            $this->ajaxReturn(0, L('operation_failure'));
        }
    }
    public function ajax_get_shop (){
        $shops = $this->_mod->get_shops ();
        exit (json_encode($shops));
    }
}